package com.liwalla.domain;

import com.liwalla.domain.DiseaseDataOnDemand;
import com.liwalla.domain.MedicineDataOnDemand;
import com.liwalla.domain.Treatement;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect TreatementDataOnDemand_Roo_DataOnDemand {
    
    declare @type: TreatementDataOnDemand: @Component;
    
    private Random TreatementDataOnDemand.rnd = new java.security.SecureRandom();
    
    private List<Treatement> TreatementDataOnDemand.data;
    
    @Autowired
    private DiseaseDataOnDemand TreatementDataOnDemand.diseaseDataOnDemand;
    
    @Autowired
    private MedicineDataOnDemand TreatementDataOnDemand.medicineDataOnDemand;
    
    public Treatement TreatementDataOnDemand.getNewTransientTreatement(int index) {
        com.liwalla.domain.Treatement obj = new com.liwalla.domain.Treatement();
        obj.setContinuous(new Boolean(true));
        obj.setDisease(diseaseDataOnDemand.getRandomDisease());
        obj.setHealed(new Boolean(true));
        obj.setMedicine(medicineDataOnDemand.getRandomMedicine());
        obj.setPosology("posology_" + index);
        return obj;
    }
    
    public Treatement TreatementDataOnDemand.getSpecificTreatement(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Treatement obj = data.get(index);
        return Treatement.findTreatement(obj.getId());
    }
    
    public Treatement TreatementDataOnDemand.getRandomTreatement() {
        init();
        Treatement obj = data.get(rnd.nextInt(data.size()));
        return Treatement.findTreatement(obj.getId());
    }
    
    public boolean TreatementDataOnDemand.modifyTreatement(Treatement obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void TreatementDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = com.liwalla.domain.Treatement.findTreatementEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Treatement' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<com.liwalla.domain.Treatement>();
        for (int i = 0; i < 10; i++) {
            com.liwalla.domain.Treatement obj = getNewTransientTreatement(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
